/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __do_commit.c                                      */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/04 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "frcomm.h"
#include "frmem.h"
#include "frame.h"
#include "client.pb.h"
#include "frsutl.h"
#include "frcmd_server.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

int __do_commit(Command *p_command, char **pp_out);

void case1(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Commit);
    p_commit_cmd->set_cmd(CommitCommand_Type_FILTER);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case2(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Commit);
    p_commit_cmd->set_cmd(CommitCommand_Type_L7);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case3(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Commit);
    p_commit_cmd->set_cmd(CommitCommand_Type_L7_DEFAULT);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}


void case4(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Commit);
    p_commit_cmd->set_cmd(CommitCommand_Type_MODULE);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}


void case5(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Commit);
    p_commit_cmd->set_cmd(CommitCommand_Type_LUA);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case6(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_SSL);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case7(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_FILTER);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case8(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_L7);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case9(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_L7_DEFAULT);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}


void case10(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_MODULE);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}


void case11(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);


    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_LUA);


    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}

void case12(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    CommitCommand *p_commit_cmd;
    char *p_args = NULL;
    char *p_out = NULL;


    p_st_cmd = new Command();
    p_st_cmd->has_commit_cmd();
    p_st_cmd->set_cmd_type(Command_CommandType_Type_CommitCommand);

    p_commit_cmd = new CommitCommand();
    p_st_cmd->set_allocated_commit_cmd(p_commit_cmd);

    p_commit_cmd->set_action(CommitCommand_Action_Rollback);
    p_commit_cmd->set_cmd(CommitCommand_Type_SSL);

    i_ret = __do_commit(p_st_cmd,&p_out);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    delete p_st_cmd;
    return;

}


int add_suite()
{
    CU_pSuite pSuite = NULL;

    pSuite = CU_add_suite("__do_commit 测试", __suite_init, __suite_cleanup);

    if (NULL == pSuite)
    {
        return 1;
    }


    //增加测试用例
    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);
    CU_add_test(pSuite, "case7", case7);
    CU_add_test(pSuite, "case8", case8);
    CU_add_test(pSuite, "case9", case9);
    CU_add_test(pSuite, "case10", case10);
    CU_add_test(pSuite, "case11", case11);
    CU_add_test(pSuite, "case12", case12);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}
